package com.tom.demo.controller.common;

import com.tom.demo.common.entity.CpTempFileBaseInfo;
import com.tom.demo.common.response.Result;
import com.tom.demo.service.common.AnalyzeSplitGenerateService;
import com.tom.demo.service.common.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "文件上传和下载")
@RestController
@RequestMapping("/common")
public class FileController {

    @Resource
    private FileService fileService;

    @Resource(name = "analyzeSplitGenerateServiceImpl")
    private AnalyzeSplitGenerateService analyzeSplitGenerateService;

    @PostMapping(path = {"/upload"})
    @ApiOperation(value = "通用文件上传")
    public Result upload(@RequestParam("file") MultipartFile file) {
        String upload = fileService.upload(file);
        return Result.success(upload);
    }


    @GetMapping(path = {"/download"})
    @ApiOperation(value = "通用文件下载")
    public ResponseEntity<org.springframework.core.io.Resource>  download(@RequestParam("fileUrl") String fileUrl) {
        fileService.download(fileUrl);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/analyzeSplitGenerate")
    @ApiOperation(value = "最新解析拆分生成单Sheet页文件")
    public Result analyzeSplitGenerate(@RequestParam(value = "files") MultipartFile[] files, HttpServletRequest httpServletRequest){
        List<CpTempFileBaseInfo> cpTempFileBaseInfos = analyzeSplitGenerateService.analyzeSplitGenerateService(files, httpServletRequest);
        return Result.success(cpTempFileBaseInfos);
    }
}
